<template>
  <div class="login-page">
        <el-form ref="form" :model="form" class="form-dialog" :rules="rules">
            <el-form-item prop="username">
                <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="el-icon-user-solid"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="form.password" show-password placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button :loading="loading" class="submit" type="primary" size="medium" @click="submit">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
// request函数
const request = (url,params) => {
    return new Promise((resolve,reject) => {
        setTimeout(() => {
            if(params.username === 'admin' && params.password === '123456') {
                resolve({
                    code:200,
                    msg:'登陆成功',
                    token:JSON.stringify(params)
                })
            } else {
                resolve({
                    code:500,
                    msg:'用户名或密码错误'
                })
            }

        },1000)
    })
}


export default {
    data() {
        return {
            loading:false,
            form: {
                username:'',
                password:''
            },
            rules: {
                username:[
                    { required: true, message: '请输入用户名', trigger: 'change' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'change' }
                ],
                password:[
                    { required: true, message: '请输入密码', trigger: 'change' },
                ]
            }
        }
    },
    methods:{
        // 登录信息函数
        async loginApi() { 
            try {
                this.loading = true
                const res = await request('/login' , this.form)
                // console.log(res)
                if(res.code === 200) {
                    this.$message.success('登陆成功')
                    localStorage.setItem('token', res.token)
                    this.$router.replace('/')
                }else {
                    this.$message.error(res.msg)
                }
            }catch(e) {
                console.log(e)
            }finally{
                this.loading = false
            }
        },
        // 点击登录按钮
        async submit() {
            try {
                await this.$refs.form.validate()
                console.log('校验通过', this.form)
                // 调取登录信息
                this.loginApi()
            } catch(e) {
                console.log('校验失败', e)
            }
        }
    }
}
</script>

<style lang="scss" scoped>
.login-page {
    width: 100vw;
    height: 100vh;
    background-image: linear-gradient(to bottom right , #73B3ED, #3F64C3);
}
.form-dialog {
    position: fixed;
    right: 100px;
    top: 50%;
    transform: translateY(-50%); 
    background: #ffffff;
    border-radius: 5px;
    padding: 20px;
    width: 300px;
    min-height: 300px;
}
.submit{
    width: 100%;
}
</style>